﻿using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections.Generic;

using System.Xml.Linq;
using System;
using Newtonsoft.Json;

public class Xml
{
    public string fileLocation = "";
    public string fileName = "";
    //小 50-100星系 中 100-300星系 大300-500星系
    public int mapSize = 1;//大小  1小2中3大
    //简单 星系里星球建筑上限提高20个  普通 星系里星球建筑上限提高0个   困难 星系里星球建筑上限减少20个
    public int mapDifficult = 1;//困难度  1简单2普通3困难
    public int mapCountyCount = 1;//国家数量
    public int fileType = 1;//1 xml 2 json 3 同时都生成
    public GalaxyMap mapData;

    //网格大坐标，小地图20*20，中地图50*50，大地图100*100
    private int[,] grid;

    //当前星系对应的网格坐标
    private int curGridX = 0, curGridY = 0;

    //当前星系数量
    int galaxyNum = 0;

    //当前星系最大数量
    int galaxyMaxNum = 0;

    //当前随机周边星系的星系id
    int currentGalaxyId = 0;

    //记录星系连线列表
    private Dictionary<string, int> galaxyLinks;

    private static Xml instance = null;

    private Xml()
    {
        //地图初始化
        mapData = new GalaxyMap();
        galaxyLinks = new Dictionary<string, int>();
    }

    public static Xml Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Xml();
            }
            return instance;
        }
    }

    public void init()
    {
        //随机数
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        //文件保存路径
        fileLocation = System.Environment.CurrentDirectory;
        if (fileType == 1)
        {
            fileName = "test";

        }
        else
        {
            fileName = "test";
        }

        //根据设置做初始化，设置地图的网格数组大小，已经开始星系的中心点网格坐标
        switch(mapSize)
        {
            //小地图
            case 1:
                galaxyMaxNum = random.Next(50, 100);
                grid = new int[50, 50];
                for (int i = 0; i < 50; i++)
                {
                    for (int j = 0; j < 50; j++)
                    {
                        grid[i, j] = -1;
                    }
                }
                curGridX = 25;
                curGridY = 25;
                break;
            //中地图
            case 2:
                galaxyMaxNum = random.Next(100, 300);
                grid = new int[80, 80];
                for (int i = 0; i < 80; i++)
                {
                    for (int j = 0; j < 80; j++)
                    {
                        grid[i, j] = -1;
                    }
                }
                curGridX = 40;
                curGridY = 40;
                break;
            //大地图
            case 3:
            default:
                galaxyMaxNum = random.Next(300, 500);
                grid = new int[100, 100];
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        grid[i, j] = -1;
                    }
                }
                curGridX = 50;
                curGridY = 50;
                break;
        }

        //初始化数据
        galaxyNum = 0;
        currentGalaxyId = 0;
        mapData.Galaxys.Clear();
        galaxyLinks.Clear();

        //创建第一个星系
        createGalaxy(galaxyNum);

        //循环创建星系的链接，其他星系在星系链接中创建
        while (currentGalaxyId < galaxyMaxNum)// && currentGalaxyId < mapData.Galaxys.Count)
        {
            createLinkGalaxy(mapData.Galaxys[currentGalaxyId]);
        }
    }

    //创建星系
    private void createGalaxy(int id)
    {
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        Galaxy galaxy = new Galaxy();
        galaxy.Name = "星系";
        galaxy.ID = id.ToString();
        galaxy.X = random.Next(curGridX * 100 + 20, (curGridX + 1) * 100 - 20).ToString();
        galaxy.Y = random.Next(curGridY * 100 + 20, (curGridY + 1) * 100 - 20).ToString();
        //galaxy.X = (curGridX * 100).ToString();
        //galaxy.Y = (curGridY * 100).ToString();
        galaxy.gridX = curGridX.ToString();
        galaxy.gridY = curGridY.ToString();
        galaxy.Type = "1 星";

        //创建星球
        int starMax = random.Next(5,10);
        for (int i = 0; i < starMax; i++)
        {
            createStar(i, galaxy);
        }

        //创建太阳
        createSun(0, galaxy);

        //记录已经生成的星系到网格数组中
        grid[curGridX, curGridY] = id;

        mapData.Galaxys.Add(galaxy);
    }

    //创建星系连线
    private void createLinkGalaxy(Galaxy galaxy)
    {
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        //周边已经生成的星系
        List<Galaxy> linkGalaxys = new List<Galaxy>();

        //从周边八格中随机生成星系
        int order = 0;//八格中的位置
        int randMax = 0;//可以生成的最大数量

        //获取相邻星系id，并将其存到周边星系变量中
        int linkGalaxyNum = getCanLinkGalaxyNum(galaxy, linkGalaxys);//返回相邻生成星系的个数
        if (linkGalaxyNum > 4) randMax = 0;
        if (linkGalaxyNum <= 4) randMax = 4 - linkGalaxyNum;

        for (int i = 0; i < randMax; i++)
        {
            order = random.Next(1 + i * 2, 3 + i*2);

            //order对应四周的八个方位，具体位置看updateGridXY方法
            if(order < 9)
            {
                //更新星系网格数组信息，并判断此时星系数量没有超过最大的星系数量
                if (updateGridXY(order, galaxy) == true && ++galaxyNum < galaxyMaxNum)
                {
                    //于是创建该星系
                    createGalaxy(galaxyNum);
                    //并记录到周边已经生成的星系列表中
                    linkGalaxys.Add(mapData.Galaxys[galaxyNum]);
                }
            }
        }

        //创建和周边星系的链接
        for (int i = 0; i < linkGalaxys.Count; i++)
        {
            //if (i > 4) break;
            //判断对角线有没有连线，并且连线没超过最大值，如果没有则做连线
            if(canLink(linkGalaxys[i], galaxy) == true)
            {
                //创建连线
                createLink(int.Parse(linkGalaxys[i].ID), galaxy);
            }
        }

        //更新当前星系标记
        currentGalaxyId = int.Parse(galaxy.ID) + 1;
    }

    //是否能连线
    private bool canLink(Galaxy galaxy1, Galaxy galaxy2)
    {
        bool result = true;

        //如果之间已经保存过单向链接则返回
        if (galaxyLinks.ContainsKey(getLinkIndex(galaxy1.ID,galaxy2.ID)))
        {
            return result;
        }

        //判断对角星系有没有连线，如果有则返回false
        if (Math.Abs(int.Parse(galaxy1.gridX) - int.Parse(galaxy2.gridX)) + Math.Abs(int.Parse(galaxy1.gridY) - int.Parse(galaxy2.gridY)) == 2)
        {
            if(int.Parse(galaxy1.gridX) > int.Parse(galaxy2.gridX))
            {
                if (int.Parse(galaxy1.gridY) > int.Parse(galaxy2.gridY))
                {
                    if (galaxyLinks.ContainsKey(getLinkIndex(grid[int.Parse(galaxy1.gridX) - 1, int.Parse(galaxy1.gridY)].ToString(), grid[int.Parse(galaxy1.gridX), int.Parse(galaxy1.gridY) - 1].ToString())))
                    {
                        result = false;
                    }
                }
                else
                {
                    if (galaxyLinks.ContainsKey(getLinkIndex(grid[int.Parse(galaxy1.gridX) - 1, int.Parse(galaxy1.gridY)].ToString(), grid[int.Parse(galaxy1.gridX), int.Parse(galaxy1.gridY) + 1].ToString())))
                    {
                        result = false;
                    }
                }
            }
            else
            {
                if (int.Parse(galaxy1.gridY) > int.Parse(galaxy2.gridY))
                {
                    if (galaxyLinks.ContainsKey(getLinkIndex(grid[int.Parse(galaxy1.gridX) + 1, int.Parse(galaxy1.gridY)].ToString(), grid[int.Parse(galaxy1.gridX), int.Parse(galaxy1.gridY) - 1].ToString())))
                    {
                        result = false;
                    }
                }
                else
                {
                    if (galaxyLinks.ContainsKey(getLinkIndex(grid[int.Parse(galaxy1.gridX) + 1, int.Parse(galaxy1.gridY)].ToString(), grid[int.Parse(galaxy1.gridX), int.Parse(galaxy1.gridY) + 1].ToString())))
                    {
                        result = false;
                    }
                }
            }
        }
        else
        {
            result = true;
        }

        //这里判断两边的链接是不是已经超过4个，超过返回false
        if (result == true)
        {
            if(getLinkNum(galaxy1) >= 4 || getLinkNum(galaxy2) >= 4)
            {
                result = false;
            }
        }

        if(result == true)
        {
            //记录到星系连线列表
            galaxyLinks[getLinkIndex(galaxy1.ID,galaxy2.ID)] = 1;
        }

        return result;
    }

    //获取星系连线列表key关键字
    private string getLinkIndex(string id1,string id2)
    {
        //对角线星系索引字串
        string index = "";
        if (int.Parse(id1) < int.Parse(id2))
        {
            index = id1 + "," + id2;
        }
        else
        {
            index = id2 + "," + id1;
        }

        return index;
    }

    //获取星系与其它星系有连线数量
    private int getLinkNum(Galaxy galaxy)
    {
        int count = 0;

        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) + 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) + 1].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) + 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) + 1].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) + 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) + 1].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY)] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY)].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY)] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY)].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) - 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) - 1].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) - 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) - 1].ToString()))) { count++; }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) - 1] != -1 && galaxyLinks.ContainsKey(getLinkIndex(galaxy.ID, grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) - 1].ToString()))) { count++; }

        return count;
    }

    //生成连接
    private void createLink(int id, Galaxy galaxy)
    {
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        Link link = new Link();
        link.MapID = "0";
        link.ID = id.ToString();
        link.X = random.Next(1, 19).ToString();
        link.Y = random.Next(1, 19).ToString();
        galaxy.Links.Add(link);
    }

    //创建星球
    private void createStar(int id, Galaxy galaxy)
    {
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        Star star = new Star();
        star.ID = id.ToString();
        star.Type = "普通行星";
        star.X = random.Next(1, 19).ToString();
        star.Y = random.Next(1, 19).ToString();

        switch (galaxy.Type)
        {
            case "1 星":
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(5, 10).ToString();
                break;
            case "2 星":
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(10, 15).ToString();
                break;
            case "3 星":
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(15, 20).ToString();
                break;
            case "4 星":
                star.MineralPer = random.Next(10, 20).ToString();
                star.GasPer = random.Next(10, 20).ToString();
                star.PopulationPer = random.Next(10, 20).ToString();
                star.BuildingCount = random.Next(25, 30).ToString();
                break;
            case "5 星":
                star.MineralPer = random.Next(30, 40).ToString();
                star.GasPer = random.Next(30, 40).ToString();
                star.PopulationPer = random.Next(30, 40).ToString();
                star.BuildingCount = random.Next(30, 40).ToString();
                break;
            case "6 星":
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(10, 15).ToString();
                break;
            case "7 星":
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(10, 15).ToString();
                break;
            default:
                star.MineralPer = random.Next(50, 100).ToString();
                star.GasPer = random.Next(50, 100).ToString();
                star.PopulationPer = random.Next(50, 100).ToString();
                star.BuildingCount = random.Next(5, 10).ToString();
                break;
        }

        star.Name = "普通行星";
        galaxy.Stars.Add(star);
    }

    //创建太阳
    private void createSun(int id, Galaxy galaxy)
    {
        Random random = new Random(System.Guid.NewGuid().GetHashCode());

        Sun sun = new Sun();
        sun.ID = id.ToString();
        sun.X = random.Next(1, 19).ToString();
        sun.Y = random.Next(1, 19).ToString();
        sun.Name = "恒星";
        galaxy.Suns.Add(sun);
    }

    //更新网格
    private bool updateGridXY(int order, Galaxy galaxy)
    {
        int tmpGridX=0,tmpGridY=0;
        int galaxyGridX = int.Parse(galaxy.gridX);
        int galaxyGridY = int.Parse(galaxy.gridY);
        switch (order)
        {
            case 1:
                tmpGridX = galaxyGridX - 1;
                tmpGridY = galaxyGridY + 1;
                break;
            case 2:
                tmpGridX = galaxyGridX;
                tmpGridY = galaxyGridY + 1;
                break;
            case 3:
                tmpGridX = galaxyGridX + 1;
                tmpGridY = galaxyGridY + 1;
                break;
            case 8:
                tmpGridX = galaxyGridX - 1;
                tmpGridY = galaxyGridY;
                break;
            case 4:
                tmpGridX = galaxyGridX + 1;
                tmpGridY = galaxyGridY;
                break;
            case 7:
                tmpGridX = galaxyGridX - 1;
                tmpGridY = galaxyGridY - 1;
                break;
            case 6:
                tmpGridX = galaxyGridX;
                tmpGridY = galaxyGridY - 1;
                break;
            case 5:
                tmpGridX = galaxyGridX + 1;
                tmpGridY = galaxyGridY - 1;
                break;
            default:
                tmpGridX = galaxyGridX;
                tmpGridY = galaxyGridY;
                break;
        }

        if(grid[tmpGridX, tmpGridY] == -1)
        {
            //如果没有生成过星系，则可以插入星系，并更新当前网格坐标
            curGridX = tmpGridX;
            curGridY = tmpGridY;

            return true;
        }
        else
        {
            return false;
        }
    }

    //获取可以连线的星系数量
    private int getCanLinkGalaxyNum(Galaxy galaxy, List<Galaxy> linkGalaxys)
    {
        int count = 0;
        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) + 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) + 1]]); }
        if (grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) + 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) + 1]]); }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) + 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) + 1]]); }
        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY)] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY)]]); }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY)] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY)]]); }
        if (grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) - 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) - 1, int.Parse(galaxy.gridY) - 1]]); }
        if (grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) - 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX), int.Parse(galaxy.gridY) - 1]]); }
        if (grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) - 1] != -1) { count++; linkGalaxys.Add(mapData.Galaxys[grid[int.Parse(galaxy.gridX) + 1, int.Parse(galaxy.gridY) - 1]]); }
        return count;
    }

    //保存地图xml信息
    public void SaveMap()
    {
        init();

        //Random random = new Random();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><GalaxyMap ID=\"0\" Name=\"\" X=\"4500\" Y=\"4500\"></GalaxyMap>");//
        // 得到根节点
        XmlNode root = xmlDoc.SelectSingleNode("GalaxyMap");

        int i = 0;
        int num = 0;
        foreach (Galaxy galaxy in mapData.Galaxys)
        {
            XmlElement xelGalaxy = xmlDoc.CreateElement("Galaxy" + i);
            XmlAttribute xelType = xmlDoc.CreateAttribute("Name");
            xelType.InnerText = galaxy.Name;
            xelGalaxy.SetAttributeNode(xelType);
            xelType = xmlDoc.CreateAttribute("ID");
            xelType.InnerText = galaxy.ID;
            xelGalaxy.SetAttributeNode(xelType);
            xelType = xmlDoc.CreateAttribute("X");
            xelType.InnerText = galaxy.X;
            xelGalaxy.SetAttributeNode(xelType);
            xelType = xmlDoc.CreateAttribute("Y");
            xelType.InnerText = galaxy.Y;
            xelGalaxy.SetAttributeNode(xelType);
            xelType = xmlDoc.CreateAttribute("Type");

            string stype = "0";
            switch (galaxy.Type)
            {
                case "1 星":
                    stype = "0";
                    break;
                case "2 星":
                    stype = "1";
                    break;
                case "3 星":
                    stype = "2";
                    break;
                case "4 星":
                    stype = "3";
                    break;
                case "5 星":
                    stype = "4";
                    break;
                case "6 星":
                    stype = "5";
                    break;
                case "7 星":
                    stype = "6";
                    break;
                default:
                    stype = "0";
                    break;
            }

            xelType.InnerText = stype;
            xelGalaxy.SetAttributeNode(xelType);
            xelType = xmlDoc.CreateAttribute("General");
            xelType.InnerText = galaxy.General;
            xelGalaxy.SetAttributeNode(xelType);

            XmlElement xelLinks = xmlDoc.CreateElement("Link");
            num = 0;
            foreach (Link link in galaxy.Links)
            {
                XmlElement xelLink = xmlDoc.CreateElement("Link" + num);
                xelType = xmlDoc.CreateAttribute("MapID");
                xelType.InnerText = link.MapID;
                xelLink.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("ID");
                xelType.InnerText = link.ID;
                xelLink.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("X");
                xelType.InnerText = link.X;
                xelLink.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Y");
                xelType.InnerText = link.Y;
                xelLink.SetAttributeNode(xelType);
                xelLinks.AppendChild(xelLink);
                num++;
            }
            xelType = xmlDoc.CreateAttribute("Count");
            xelType.InnerText = num.ToString();
            xelLinks.SetAttributeNode(xelType);
            xelGalaxy.AppendChild(xelLinks);

            XmlElement xelStars = xmlDoc.CreateElement("Star");
            num = 0;
            
            //自动生成星球
            //int k = 0;
            //for (k = 0; k < 10; k++ )
            //{
            //    Star item = new Star();
            //    item.ID = k.ToString();
            //    item.Type = "2";
            //    item.X = random.Next(1, 19).ToString();
            //    item.Y = random.Next(1, 19).ToString();

            //    switch (galaxy.Type)
            //    {
            //        case "1 星":
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(5, 10).ToString();
            //            break;
            //        case "2 星":
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(10, 15).ToString();
            //            break;
            //        case "3 星":
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(15, 20).ToString();
            //            break;
            //        case "4 星":
            //            item.MineralPer = random.Next(10, 20).ToString();
            //            item.GasPer = random.Next(10, 20).ToString();
            //            item.PopulationPer = random.Next(10, 20).ToString();
            //            item.BuildingCount = random.Next(25, 30).ToString();
            //            break;
            //        case "5 星":
            //            item.MineralPer = random.Next(30, 40).ToString();
            //            item.GasPer = random.Next(30, 40).ToString();
            //            item.PopulationPer = random.Next(30, 40).ToString();
            //            item.BuildingCount = random.Next(30, 40).ToString();
            //            break;
            //        case "6 星":
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(10, 15).ToString();
            //            break;
            //        case "7 星":
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(10, 15).ToString();
            //            break;
            //        default:
            //            item.MineralPer = "0";
            //            item.GasPer = "0";
            //            item.PopulationPer = "0";
            //            item.BuildingCount = random.Next(5, 10).ToString();
            //            break;
            //    }
                
            //    item.Name = galaxy.Name + k;
            //    galaxy.Stars.Add(item);
            //}

            foreach (Star star in galaxy.Stars)
            {
                XmlElement xelStar = xmlDoc.CreateElement("Star" + num);
                xelType = xmlDoc.CreateAttribute("Type");

                switch (star.Type)
                {
                    case "蓝巨星":
                        stype = "1";
                        break;
                    case "普通行星":
                        stype = "2";
                        break;
                    default:
                        stype = "2";
                        break;
                }
                xelType.InnerText = stype;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("X");
                xelType.InnerText = star.X;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Y");
                xelType.InnerText = star.Y;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("MineralPer");
                xelType.InnerText = star.MineralPer;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("GasPer");
                xelType.InnerText = star.GasPer;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("PopulationPer");
                xelType.InnerText = star.PopulationPer;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("BuildingCount");
                xelType.InnerText = star.BuildingCount;
                xelStar.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Name");
                xelType.InnerText = star.Name;
                xelStar.SetAttributeNode(xelType);
                xelStars.AppendChild(xelStar);
                num++;
            }
            xelType = xmlDoc.CreateAttribute("Count");
            xelType.InnerText = num.ToString();
            xelStars.SetAttributeNode(xelType);
            xelGalaxy.AppendChild(xelStars);

            XmlElement xelSuns = xmlDoc.CreateElement("Sun");
            num = 0;
            foreach (Sun sun in galaxy.Suns)
            {
                XmlElement xelSun = xmlDoc.CreateElement("Sun" + num);
                xelType = xmlDoc.CreateAttribute("X");
                xelType.InnerText = sun.X;
                xelSun.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Y");
                xelType.InnerText = sun.Y;
                xelSun.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Name");
                xelType.InnerText = sun.Name;
                xelSun.SetAttributeNode(xelType);
                xelSuns.AppendChild(xelSun);
                num++;
            }
            xelType = xmlDoc.CreateAttribute("Count");
            xelType.InnerText = num.ToString();
            xelSuns.SetAttributeNode(xelType);
            xelGalaxy.AppendChild(xelSuns);

            XmlElement xelTreasures = xmlDoc.CreateElement("Treasure");
            num = 0;
            foreach (Treasure treasure in galaxy.Treasures)
            {
                XmlElement xelTreasure = xmlDoc.CreateElement("Treasure" + num);
                xelType = xmlDoc.CreateAttribute("X");
                xelType.InnerText = treasure.X;
                xelTreasure.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Y");
                xelType.InnerText = treasure.Y;
                xelTreasure.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Technology");
                xelType.InnerText = treasure.Technology;
                xelTreasure.SetAttributeNode(xelType);
                xelType = xmlDoc.CreateAttribute("Name");
                xelType.InnerText = treasure.Name;
                xelTreasure.SetAttributeNode(xelType);
                xelTreasures.AppendChild(xelTreasure);
                num++;
            }
            xelType = xmlDoc.CreateAttribute("Count");
            xelType.InnerText = num.ToString();
            xelTreasures.SetAttributeNode(xelType);
            xelGalaxy.AppendChild(xelTreasures);

            i++;
            root.AppendChild(xelGalaxy);
        }

        xmlDoc.Save(fileLocation + "\\" + fileName + ".xml");

        //xml转json方法之一，不太好用
        //string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc);
        //StreamWriter FileWriter = new StreamWriter(fileLocation + "\\" + fileName + ".json", true); //写文件
        //FileWriter.Write(json);//将字符串写入
        //FileWriter.Close(); //关闭StreamWriter对象
    }
}
